package pbbltobpel.parser;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class PlanStep implements IXmlReader{
	private List<String> params;
	private int id;
	private String name;
	private int ps;
	private boolean isNew;
	private int number;
	private PddlPlan plan;
	private List<Fact> adds;
	private List<Fact> deletes;
	private List<Fact> preconditions;
	
	public PlanStep() {
		params = new ArrayList<String>();
		adds = new ArrayList<Fact>();
		deletes = new ArrayList<Fact>();
		preconditions = new ArrayList<Fact>();
	}
	
	public List<String> getParams() {
		return params;
	}
	public void setParams(List<String> params) {
		this.params = params;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public int getPs() {
		return ps;
	}

	public void setPs(int ps) {
		this.ps = ps;
	}

	public boolean isNew() {
		return isNew;
	}

	public void setNew(boolean isNew) {
		this.isNew = isNew;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public List<Fact> getPreconditions() {
		return preconditions;
	}

	public void setPreconditions(List<Fact> preconditions) {
		this.preconditions = preconditions;
	}

	public List<Fact> getDeletes() {
		return deletes;
	}

	public void setDeletes(List<Fact> deletes) {
		this.deletes = deletes;
	}

	public List<Fact> getAdds() {
		return adds;
	}

	public void setAdds(List<Fact> adds) {
		this.adds = adds;
	}
	
	@Override
	public void getFromXmlElement(Element el) {
		id = Integer.parseInt(el.getAttribute("id"));
		name = el.getAttribute("name");
		ps =  Integer.parseInt(el.getAttribute("ps"));
		number = Integer.parseInt(el.getAttribute("number"));
		String newValue = el.getAttribute("new");
		isNew = "true".equals(newValue);
		NodeList p = el.getElementsByTagName("param");
		for(int i = 0; i < p.getLength(); i++){
			Node node = p.item(i);
			String text = node.getTextContent();
			params.add(text);
		}
		
		setFacts(adds, "adds", el);
		setFacts(deletes, "deletes", el);
		setFacts(preconditions, "preconditions", el);
	}
	
	private void setFacts(List<Fact> factsList, String name, Element el) {
		Element e = (Element)el.getElementsByTagName(name).item(0);
		NodeList list = e.getElementsByTagName("fact");
		for(int i = 0; i < list.getLength(); i++) {
			Element fact = (Element)list.item(i);
			int id = Integer.parseInt(fact.getAttribute("id"));
			List<Fact> facts = plan.getFacts();
			for(int j = 0; j < facts.size(); j++) {
				Fact f = facts.get(j);
				if(f.getId() == id) {
					factsList.add(f);
					break;
				}
			}
		}
	}

	public PddlPlan getPlan() {
		return plan;
	}

	public void setPlan(PddlPlan plan) {
		this.plan = plan;
	}
}
